﻿// HSS.Interlink.Program.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2012
// ----------------------------------------------------------------------------
// File:       Program.cs
// Author:     HSS\gbanta
// Created:    01/17/2012
// Modified:   01/17/2012
// ----------------------------------------------------------------------------
using System;
using System.IO;
using System.Threading;

namespace HSS.Interlink
{
	class Program
	{
		static System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
		static UploadClient client;
		static ManualResetEvent waiter = new ManualResetEvent(false);
		static ManualResetEvent creator = new ManualResetEvent(false);
		static bool uploading = false;

		[STAThread]
		static void Main(string[] args)
		{
			Console.TreatControlCAsInput = false;
			Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
			//new Thread(CreateClient).Start();
			//new Thread(Process).Start();
			CreateClient();
			Process();
			Console.WriteLine("Done!");
		}

		static void CreateClient()
		{
			client = new UploadClient();
			client.Initialize("http://localhost:22022/FileUpload.ashx", "UploadHandler", true);
			client.UploadProgressChanged += client_UploadProgressChanged;
			client.UploadCompleted += client_UploadCompleted;
			creator.Set();
		}

		static void Process()
		{
			creator.WaitOne();
			while (true)
			{
				waiter.Reset();

				Console.WriteLine("");
				Console.WriteLine("press any key or X to exit . . .");
				var k = Console.ReadKey(true);
				if (k.Key == ConsoleKey.X)
					return;

				Start();

				Console.WriteLine("");
				Console.WriteLine("Uploading file; press CTL+C to cancel ...");
				Console.WriteLine("");

				waiter.WaitOne();
			}
		}

		static void Start()
		{
			Console.WriteLine("");
			Console.WriteLine("Start: " + Thread.CurrentThread.ManagedThreadId);
			var fs = File.Open("c:\\users\\gbanta\\desktop\\test.zip", FileMode.Open);
			client.UploadStreamAsync(fs, fs.Name, "", true, Thread.CurrentThread.ManagedThreadId);
			watch.Start();
			uploading = true;
		}

		static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
		{
			try
			{
				if (uploading)
				{
					e.Cancel = true;
					client.CancelAsync();
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine("Cancel request exception: " + ex.Message);
			}
		}

		static void client_UploadCompleted(object sender, UploadFileCompletedEventArgs e)
		{
			try
			{
				uploading = false;

				Console.WriteLine("UploadCompleted: " + Thread.CurrentThread.ManagedThreadId);
				Console.WriteLine("UserToken: " + e.UserState);
				Console.WriteLine("");

				watch.Stop();
				Console.WriteLine("TotalSeconds: " + watch.Elapsed.TotalSeconds);
				watch.Reset();

				if (e.Cancelled)
				{
					Console.WriteLine("Result: Canceled");
					return;
				}
				if (null != e.Error)
				{
					Console.WriteLine("Result: " + e.Error.Message);
					return;
				}
				Console.WriteLine("Result: " + e.Result);
			}
			finally
			{
				waiter.Set();
			}
		}
		static void client_UploadProgressChanged(object sender, UploadFileProgressEventArgs e)
		{
			Console.WriteLine("UploadProgressChanged: " + Thread.CurrentThread.ManagedThreadId);

			//Console.WriteLine("BytesRemaining: " + e.BytesRemaining);
			//Console.WriteLine("BytesSent: " + e.BytesSent);
			Console.WriteLine("ProgressPercentage: " + e.ProgressPercentage);
			//Console.WriteLine("TotalBytesToSend: " + e.TotalBytesToSend);
			//Console.WriteLine("UserState: " + e.UserState);
		}
	}
}
